package com.bosssoft.goods.service.impl;

import com.bosssoft.goods.dao.ShoppingTrolleyDao;
import com.bosssoft.goods.domain.dto.StGoodsItemDTO;
import com.bosssoft.goods.domain.vo.StBatchGoodOperateVO;
import com.bosssoft.goods.domain.vo.StGoodUpdateVO;
import com.bosssoft.goods.service.ShoppingTrolleyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品购物车服务接口实现类
 * @author Fang_Sir
 * @date 2021-04-21
 */
@Service
public class ShoppingTrolleyServiceImpl implements ShoppingTrolleyService {
    @Autowired
    private ShoppingTrolleyDao shoppingTrolleyDao;

    @Override
    public boolean addGood(StGoodUpdateVO info) {
        if(shoppingTrolleyDao.isAddedGood(info.getUid(), info.getGoodId()) > 0){
            return shoppingTrolleyDao.updateQty(info.getUid(), info.getGoodId(), info.getQty()) > 0;
        } else {
            return shoppingTrolleyDao.addGood(info) > 0;
        }
    }

    @Override
    public List<Long> addGood(List<StGoodUpdateVO> goods) {
        List<Long> err= new ArrayList<>();
        goods.forEach(e -> {
            if(e.getQty() == 0) {
                return;
            }
            if(e.getQty() < 0 || !this.addGood(e)){
                err.add(e.getGoodId());
            }
        });
        return err;
    }

    @Override
    public List<StGoodsItemDTO> getShoppingTrolleyWithGoodInfo(long uid) {
        return shoppingTrolleyDao.getShoppingTrolleyList(uid);
    }

    @Override
    public List<StGoodsItemDTO> getBatchGoodsInfo(StBatchGoodOperateVO info) {
        if(info.getGoodIds().length <= 0 ) {
            return new ArrayList<>();
        }
        return shoppingTrolleyDao.getStListByGoodIds(info.getUid(),info.getGoodIds());
    }

    @Override
    public boolean updateGoodQty(StGoodUpdateVO info) {
        if(info.getQty() > 0) {
            return shoppingTrolleyDao.updateQty(info.getUid(), info.getGoodId(), info.getQty()) > 0;
        }
        return false;
    }

    @Override
    public boolean removeGood(StBatchGoodOperateVO info) {
        if(info.getGoodIds().length <= 0) {
            return true;
        }
        return shoppingTrolleyDao.removeGood(info.getUid(), info.getGoodIds()) > 0;
    }

}
